/*
* Copyright (C) 2020 ~ 2024 chaigec All Rights Reserved.
*
* Author:     chaigec <chaigec@163.com>
* Maintainer: chaigec <chaigec@163.com>

 * Use of this source code is governed by MIT license that can be found in the
 * LICENSE file in the root of the source tree. All contributing project authors
 * may be found in the AUTHORS file in the root of the source tree.
*/

#include "MkRtspRequest.h"
#include "MkUtil/MkUtil.h"
#include "MkUtil/MkLog.h"
#include "MkRtspUtil.h"

CMkRtspRequest::CMkRtspRequest()
    :m_Version("1.0")
{

}

Uint32 CMkRtspRequest::OnPacketHeaderLine(OUT MkString& strHeader)
{
    MkString strMethod = CMkRtspUtil::GetInstance().GetRtspMethod(m_Method);
    if ("1.0" != m_Version || strMethod.empty()) {
        MkErrorLog("method:%d or version:%s is invalid\n", m_Method, m_Version.c_str());
        return InvalidParams;
    }
    strHeader = strMethod + " " + m_Uri + " RTSP/" + m_Version;
    return NoneError;
}

Uint32 CMkRtspRequest::OnParseHeaderLine(const MkString& strHeader)
{
    MkVector<MkString> VecStr;
    Uint32 ErrorCode = CMkUtil::StringSplit(strHeader, " ", VecStr);
    MkCheckErrorLog(ErrorCode, "split header:%s failed\n", strHeader.c_str());
    if (VecStr.size() < 3) {
        MkErrorLog("rtsp request header:%s invalid \n", strHeader.c_str());
        return InvalidParams;
    }
    m_Method = CMkRtspUtil::GetInstance().GetMkMethod(VecStr[0]);
    if (MkRtspMethodUnknown == m_Method) {
        MkErrorLog("got a error method:%d\n", VecStr[0].c_str());
        return InvalidParams;
    }
    m_Uri = VecStr[1];
    size_t Post = VecStr[2].find("RTSP/");
    if (MkString::npos == Post) {
        MkErrorLog("rtsp request header version(%s) invalid\n", VecStr[2].c_str());
    }
    m_Version = VecStr[2].substr(Post);
    return NoneError;
}

CMkRtspRequest::~CMkRtspRequest()
{

}